package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @GetMapping("/list")
    public R<List<ShoppingCart>> getList(HttpServletRequest request){
        Long user = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,user);
        shoppingCartLambdaQueryWrapper.orderByDesc(ShoppingCart::getCreateTable);
        //购物车可能有多个，所以查询是应该用的是list
        List<ShoppingCart> list = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
        return R.success(list);
    }

    @PostMapping("/add")
    public R<ShoppingCart> addCart(HttpServletRequest request, @RequestBody ShoppingCart shoppingCart){
            Long user = (Long) request.getSession().getAttribute("user");
            shoppingCart.setUserId(user);
            LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
           shoppingCartLambdaQueryWrapper.eq(user != null, ShoppingCart::getUserId, user);
            if(shoppingCart.getDishId()!=null){
                shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            }else{
                shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            }
        ShoppingCart one = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
            log.info("one:"+one);
            if(one!=null){
                Integer count = one.getNumber();
                log.info("count:"+count);
                count++;
                one.setNumber(count);
                boolean b = shoppingCartService.updateById(one);
                if(!b){
                   return R.error("购物车添加失败");
                }
            }else{
                //如果直接使用one会造成空指针异常。
                shoppingCart.setCreateTable(LocalDateTime.now());
                shoppingCart.setNumber(1);
                boolean save = shoppingCartService.save(shoppingCart);
                if(!save){
                    return R.error("购物车添加失败");
                }
                //这里用one而不是用shoppingCart，是因为代码的优雅型，用one，上一个判断语句就不用写return了。
                one=shoppingCart;
            }
            return R.success(one);
    }

    @PostMapping("/sub")
    public R<String> updateCartApi(@RequestBody ShoppingCart shoppingCart){
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        log.info("dishId"+dishId);
        log.info("setmealId"+setmealId);
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(dishId!=null){
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
            ShoppingCart one = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
            Integer number = one.getNumber();
            if(number==1){
                boolean remove = shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
                if(!remove){
                    return R.error("购物车删除失败");
                }
            }
            if(number>1){
                number--;
                one.setNumber(number);
                boolean b = shoppingCartService.updateById(one);
                if(!b){
                    return R.error("购物车删除失败");
                }
            }
            return R.success("菜品删除成功");
        }else{
                    shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
            ShoppingCart one = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
            Integer number = one.getNumber();
            if(number==1){
                boolean remove = shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
                if(!remove){
                    return R.error("购物车删除失败");
                }
            }
            if(number>1){
                number--;
                one.setNumber(number);
                boolean b = shoppingCartService.updateById(one);
                if(!b){
                    return R.error("购物车删除失败");
                }
            }
                    return R.success("菜品删除成功");
        }
    }

    @DeleteMapping("/clean")
    public R<String> cleanShoppingCart(HttpServletRequest request){
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //要用的user的id进行删除，不能直接删除不然就把所有用户的购物车都删除了。
        Long user = (Long) request.getSession().getAttribute("user");
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,user);
        boolean remove = shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
        if(!remove){
            return R.error("清空失败");
        }
        return R.success("清空成功");
    }
}
